package com.hxlc.util;

import com.google.common.base.Joiner;

import java.util.*;

/**
 * 字符串工具类
 */
public class HxStringUtils {

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
    /**
     *
     * @param str
     * @return false 为空  true 为非空
     */
    public static boolean isNotBlank(String str){
        if (str == null || str.trim().equals("")){
            return false;
        }
        return true;
    }



    /**
     *
     * @param obj
     * @return false 为空  true 为非空
     */
    public static boolean isNotBlank(Object obj){
        if (obj == null || obj.toString().trim().equals("")){
            return false;
        }
        return true;
    }

    public static String remove(String str, String remove) {
        return !isEmpty(str) && !isEmpty(remove) ? replace(str, remove, "", -1) : str;
    }

    public static String join(String[] strArr,String splitFlag){
        String strs = "";
        for (String str:strArr){
            strs = strs+str+splitFlag;
        }
        return strs.substring(0,strs.length()-splitFlag.length());
    }
    public static String join(Collection<String> coll, String split) {
        if (coll.isEmpty()) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            boolean isFirst = true;

            String s;
            for(Iterator var4 = coll.iterator(); var4.hasNext(); sb.append(s)) {
                s = (String)var4.next();
                if (isFirst) {
                    isFirst = false;
                } else {
                    sb.append(split);
                }
            }

            return sb.toString();
        }
    }

    /**
     * 利用分隔符进行转换
     * @param str
     * @param flag
     * @param isBlank 是否包含非空
     * @return
     */
    public static List<String> splitList(String str, String flag,boolean isBlank) {
        if (!HxStringUtils.isNotBlank(str)){
            return new ArrayList<String>();
        }
        List<String> strList = new ArrayList<String>();
        if (!HxStringUtils.isNotBlank(flag)){
            strList.add(str);
            return strList;
        }
        String[] strArr = str.split(flag);
        for (String s : strArr) {
            if (!isBlank){
                if (HxStringUtils.isNotBlank(s)){
                    strList.add(s.trim());
                }else {
                    continue;
                }
            }else {
                strList.add(s.trim());
            }
        }
        return strList;
    }

    /**
     * 去除集合中空白字符串
     * @param stringList
     */
    public static List<String> removeIsBank(List<String> stringList) {
        if (!CollectionUtils.isNotBlank(stringList)){
            return null;
        }
        List<String> newList = new ArrayList<String>();
        for (int i = 0; i < stringList.size(); i++) {
            String s = stringList.get(i);
            if (isNotBlank(s)){
                s = s.trim();
            }else {
                continue;
            }
            newList.add(s);
        }
        return newList;
    }


    public static String rightPad(String str, int size, String padStr) {
        if (str == null) {
            return null;
        } else {
            if (isEmpty(padStr)) {
                padStr = " ";
            }

            int padLen = padStr.length();
            int strLen = str.length();
            int pads = size - strLen;
            if (pads <= 0) {
                return str;
            } else if (padLen == 1 && pads <= 8192) {
                return rightPad(str, size, String.valueOf(padStr.charAt(0)));
            } else if (pads == padLen) {
                return str.concat(padStr);
            } else if (pads < padLen) {
                return str.concat(padStr.substring(0, pads));
            } else {
                char[] padding = new char[pads];
                char[] padChars = padStr.toCharArray();

                for(int i = 0; i < pads; ++i) {
                    padding[i] = padChars[i % padLen];
                }

                return str.concat(new String(padding));
            }
        }
    }

    public static String replace(String text, String searchString, String replacement, int max) {
        if (!isEmpty(text) && !isEmpty(searchString) && replacement != null && max != 0) {
            int start = 0;
            int end = text.indexOf(searchString, start);
            if (end == -1) {
                return text;
            } else {
                int replLength = searchString.length();
                int increase = replacement.length() - replLength;
                increase = increase < 0 ? 0 : increase;
                increase *= max < 0 ? 16 : (max > 64 ? 64 : max);

                StringBuilder buf;
                for(buf = new StringBuilder(text.length() + increase); end != -1; end = text.indexOf(searchString, start)) {
                    buf.append(text.substring(start, end)).append(replacement);
                    start = end + replLength;
                    --max;
                    if (max == 0) {
                        break;
                    }
                }

                buf.append(text.substring(start));
                return buf.toString();
            }
        } else {
            return text;
        }
    }

    public static String getString(Object obj) {
        if (obj == null){
            return null;
        }
        return obj.toString();
    }

    /**
     * 分发方式getOnlyKey
     * @param params
     * @return
     */
    public static String getToString(String ... params){
        if (params == null || params.length == 0){
            return "";
        }
        String toString = "";
        for (int i = 0; i < params.length; i++) {
           String start =  params[i];
           if(isNotBlank(params[i])){
               toString = toString+params[i];
            }
        }
        return toString;
    }

    /**
     *
     * @param number
     * @param num
     * @return
     */
    public static String formatString(Integer number,int num) {
        return String.format("%0"+num+"d",number);
    }


    /**
     *
     * @param str
     * @param num
     * @return
     */
    public static String formatString(String str,int num) {
        return String.format("%"+num+"s",str);
    }

    /**
     * 时间合并
     * @param year
     * @param days
     * @param hours
     * @return
     */
    public static String dateJoin(Integer year,Integer days,Integer hours){
        return formatString(year,4)+formatString(days,4)+formatString(hours,2)+"0000";
    }

    /**
     *
     * @param obj
     * @return false 为空  true 为非空
     */
    public static String getStringByObj(Object obj){
        if (obj == null || obj.toString().trim().equals("")){
            return null;
        }
        return obj.toString();
    }

    /**
     * 判断传入的参数是否等于后面其中一个
     * @param data
     * @param res
     * @return
     */
    public static boolean verdictEqualitySomeOne(Integer data,Integer ...res){
        for(int i = 0 ;i<res.length;i++){
            if(data.equals(res[i])){
                return true;
            }
        }
        return false;
    }
    /**
     * 判断传入的参数是否等于后面其中一个
     * @param data
     * @param res
     * @return
     */
    public static boolean verdictEqualitySomeOne(String data,String  ...res){
        for(int i = 0 ;i<res.length;i++){
            if(data.equals(res[i])){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据传入值和分隔符拼接字符串
     */
    public static String getToStringBySlice(String slice,String ...data){
        List<String> strings = new ArrayList<>();
        for(int i = 0 ;i<data.length;i++){
            strings.add(data[i]);
        }
        strings.removeAll(Collections.singleton(null));
        return Joiner.on(slice).join(strings);
    }
}
